package gl.uitls;

import gl.App;
import gl.android.biyiqing.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.gitonway.lee.niftynotification.lib.Configuration;
import com.gitonway.lee.niftynotification.lib.Effects;
import com.gitonway.lee.niftynotification.lib.NiftyNotificationView;

public class Tip {
	static Configuration cfg;
	static {
		cfg = new Configuration.Builder().setAnimDuration(700)
				.setDispalyDuration(500).setBackgroundColor("#00f88e1e")
				.setTextColor("#FFffffff").setIconBackgroundColor("#FFFFFFFF")
				.setTextPadding(5) // dp
				.setViewHeight(48) // dp
				.setTextLines(2) // You had better use setViewHeight and
									// setTextLines together
				.setTextGravity(Gravity.CENTER) // only text def
												// Gravity.CENTER,contain icon
												// Gravity.CENTER_VERTICAL
				.build();
		// show(boolean) allow duplicates or showSticky() sticky
		// notification,you can call removeSticky() method close it
	}

	@SuppressLint("InflateParams")
	public static void error(Context context, String msg) {
		LayoutInflater myInflater = LayoutInflater.from(context);
		View view = myInflater.inflate(R.layout.toast, null);
		Button button = (Button) view.findViewById(R.id.button);
		button.setText(msg);
		button.setTextColor(0xf00);
		Toast mytoast = new Toast(context);
		mytoast.setGravity(Gravity.BOTTOM, 0, 128);
		mytoast.setView(view);
		mytoast.setDuration(Toast.LENGTH_SHORT);
		mytoast.show();
	}

	@SuppressLint("InflateParams")
	public static void info(Context context, String msg) {
		LayoutInflater myInflater = LayoutInflater.from(context);
		View view = myInflater.inflate(R.layout.toast, null);

		Button button = (Button) view.findViewById(R.id.button);
		button.setText(msg);

		Toast mytoast = new Toast(context);
		mytoast.setGravity(Gravity.BOTTOM, 0, 128);
		mytoast.setView(view);
		mytoast.setDuration(Toast.LENGTH_SHORT);
		mytoast.show();

	}

	public static NiftyNotificationView genStickyTip(Activity context, Object msg) {

		NiftyNotificationView view  = NiftyNotificationView
				.build(context, msg.toString(), Effects.standard,
						R.id.toast, cfg);
				// remove this line ,only text
//				.setIcon(R.drawable.ic_launcher)
//				.setOnClickListener(new View.OnClickListener() {
//					@Override
//					public void onClick(View view) {
//						// add your code
//					}
//				});
				return view;
	}
	public static void dismissStickNotif(final NiftyNotificationView view) {
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				try {
					view.removeSticky();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, 1000);
		
	}
	public static void showStickNotif(NiftyNotificationView view) {
		try {
			if (view ==null) {
				return;
			}
			view.showSticky();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public static void show(Object message) {
		info(App.applicationContext, "" + message);
	}

	public static void show(int RstringID) {
		info(App.applicationContext,
				App.applicationContext.getString(RstringID));
	}

	public static void show(View hostView, View contentView) {
		PopupWindow pop = new PopupWindow(contentView, hostView.getWidth(),
				LayoutParams.WRAP_CONTENT, true);
		pop.setBackgroundDrawable(new ColorDrawable(0x00000000));
		pop.showAsDropDown(hostView, 0, 0);
		pop.setOutsideTouchable(true);
	}

	public static void showTipDialog(Context content, String message,
			String title, DialogInterface.OnClickListener yesOnclickImp) {
		new AlertDialog.Builder(content).setMessage(message)
				.setPositiveButton("确定", yesOnclickImp).setTitle(title).show();

	}

	public static void showEnterDialog(Context content, String message,
			String title, DialogInterface.OnClickListener yesOnclickImp) {
		new AlertDialog.Builder(content).setMessage(message)
				.setPositiveButton("确定", yesOnclickImp)
				.setNeutralButton("否", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();

					}
				}).setTitle(title).show();

	}
}
